package com.cy.server;

import java.io.IOException;
import java.io.InputStream;

/**
 * @Author ChenYan
 * @Date 2021/8/13 13:42
 * @Description 请求对象
 */
public class Request {

    /**
     * GET/POST
     */
    private String method;

    /**
     * url
     */
    private String url;

    /**
     * 输入流
     */
    private InputStream inputStream;

    public Request(InputStream inputStream) throws IOException {
        this.inputStream = inputStream;

        int count = 0;
        while (count == 0){
            count = inputStream.available();
        }
        byte[] bytes = new byte[count];
        inputStream.read(bytes);

        String inputStr = new String(bytes);
        String[] inputStrs = inputStr.split("\\n");
        String firstLine = inputStrs[0];

        String[] firstLineStrs = firstLine.split(" ");
        this.method = firstLineStrs[0];
        this.url = firstLineStrs[1];

        System.out.println("Request: method = " + method + " url = " + url);
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }
}
